
//
//  LBStatusCell.swift
//  LBNewWeiBo
//
//  Created by bin on 15/11/1.
//  Copyright © 2015年 bin. All rights reserved.
//

import UIKit

let StatusCellMarign: CGFloat = 8.0

/// 自定义微博内容展示cell
class LBStatusCell: UITableViewCell {


    //MARK: - 属性
    /// 设置配图内容的宽约束
    var PictureViewWidthConst: NSLayoutConstraint?
    /// 设置配图内容的高约束
    var PictureViewHeightConst: NSLayoutConstraint?
    
    /// 模型
    var status: LBStatus? {
        didSet {
            // 将 模型赋值给头部视图的模型
            topView.status = status
            // 配图模型
            PictureView.status = status
            
            let pictureSize = PictureView.calcViewSize()
            
            print("pictureSize:\(pictureSize)")
            
            // 根据配图的尺寸来约束配图内容
            PictureViewWidthConst?.constant = pictureSize.width
            PictureViewHeightConst?.constant = pictureSize.height
            // 为微博内容赋值
            contentLable.text = status?.text
        }
    }
    
    
    //MARK: -  构造方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 准备UI
        prepareUI()
    }

    //MARK: - 准备UI
    private func prepareUI() {
        
        // 添加子控件
        contentView.addSubview(topView) // 添加头部视图
        contentView.addSubview(contentLable) // 添加微博内容
        contentView.addSubview(PictureView) // 配图内容
        contentView.addSubview(buttomView) // 添加底部视图
        // 设置约束
        
        // 头部视图的约束
       topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44))
        
        
        // 微博内容的约束
        contentLable.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size:nil, offset: CGPoint(x: 8, y: 8))
        
        // 微博内容视图的宽高
        contentView.addConstraint(NSLayoutConstraint(item: contentLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
        
        // 微博配图
        let cons = PictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLable, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: StatusCellMarign))
        
        // 设置配图的宽高
        PictureViewHeightConst = PictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        PictureViewWidthConst = PictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        
        // 设置 底部视图的约束 和 微博内容的下左约束
        buttomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: PictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -StatusCellMarign, y: StatusCellMarign))
        
        // 底部视图 和 微博视图的底部重合
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }

    //MARK: - 懒加载
    /// 头部视图
    lazy var topView : LBStatusTopView = LBStatusTopView()
    
    /// content 微博内容
    lazy var contentLable: UILabel = {
        // 创建文字
        let lable = UILabel(fontSize: 14, textColor:UIColor.darkGrayColor())
        // 设置自动换行
        lable.numberOfLines = 0
//        lable.sizeToFit()
        
        return lable
    }()
    
    /// 微博配图
    lazy var PictureView = LBStatusPictureView()
    
    /// 底部视图
    lazy var buttomView: LBStatusBottomView = LBStatusBottomView ()

}
